package net.zjitc.servlet.cart;

import net.zjitc.daomain.Book;
import net.zjitc.service.BookService;
import net.zjitc.service.BookServiceImpl.BookServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by hasee on 2017/10/15.
 */
@WebServlet(name = "DeleteServlet",urlPatterns = "/deleteStrvlet")
public class DeleteServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        BookService bookService = new BookServiceImpl();
        List<Book> list = (List) request.getSession().getAttribute("cart");
        Book book = null;
        for (int i = 0; i<list.size() ; i++) {
            book = list.get(i);
            if(id.equals(book.getId())){
                list.remove(i);
                request.getSession().setAttribute("cart",list);
                break;
            }
        }
        response.sendRedirect("/web/cart/cart.jsp");
    }
}
